<?php

namespace app\common\model\offer;

use app\common\model\BaseModel;
use app\system\controller\offer\GoodsConfigController;
use helper\Tree;


class GoodsModel extends BaseModel
{
    protected $table = 'offer_goods';




    /**
     * 返回分类列表
     * @param boolean $refresh
     * @return array
     * @throws \Exception
     */
    public function getList($refresh = false) {
        if (cache("?offer_goods") && $refresh == false) return cache('offer_goods');
        $list = $this->order("show_order asc")->select()->toArray();
        foreach($list as $k=>$v){
            $offerConfig = '';
            if(!empty($v['goods_config_ids'])){
                $offerArr = GoodsConfigModel::where('id','in',$v['goods_config_ids'])
                    //->orderRaw("field(id,{$v['goods_config_ids']})")
                    ->column('goods_name');

                $offerConfig = implode(',',$offerArr);
            }
            $list[$k]['offer_config'] = $offerConfig;
        }
        $class = $this->formatTree($list,'goods_name');
        cache('offer_goods', $class);
        return $class;
    }

    /**
     * 获取树型栏目
     * @param false $refresh
     * @return array
     * @throws \Exception
     */
    public function getTree($refresh = false)
    {
        $list = $this->getList($refresh);
        return Tree::channelLevel($list);
    }

    /**
     * 获取指定栏目
     * @param $id
     * @return mixed
     * @throws \Exception
     */
    public function getInfo($id){
        $list = $this->getList();
        if (!isset($list[$id])) exception('参数错误');
        return $list[$id];
    }

    /**
     * 添加分类
     * @param $data
     * @return \think\Model
     * @throws \Exception
     */
    public function add($data) {
        $info = self::create($data);
        $this->getList(true);
        return $info;
    }

    /**
     * 编辑分类
     * @param $data
     * @return \think\Model
     * @throws \Exception
     */
    public function edit($data) {
        $info = self::update($data);
        $this->getList(true);
        return $info;
    }

    /**
     * 删除分类
     * @param $id
     * @throws \Exception
     */
    public function del($id) {
        if ($this->where('pid', $id)->find()) exception('存在下级分类');
        if (GoodsDetailModel::where('goods_id', $id)->find()) exception('该分类下存在对应明细');
        $this->where('id', $id)->delete();
        $this->getList(true);
    }

    public function goodsList(){
        $list = $this->getList();
        $list = Tree::channelLevel($list);
        $configModel = new GoodsConfigModel();
        $goodsConfigList = $configModel->column('*','id');
        $goodsConfigList = $configModel->_formatList($goodsConfigList);
        foreach($list as $k=>$v){
           if(!empty($v['children'])){
                $children = $v['children'];
                foreach ($children as $kk=>$vv){
                    $configIds = explode(',',$vv['goods_config_ids']);
                    foreach ($configIds as $id){
                        $vv['goods_config'][$id] = $goodsConfigList[$id] ?? [];
                    }
                    $v['children'][$kk] = $vv;
                }

            }else{
                $configIds = explode(',',$v['goods_config_ids']);
                foreach ($configIds as $id){
                    $v['goods_config'][$id] = $goodsConfigList[$id] ?? [];
                }
            }
            $list[$k] = $v;
        }
        return $list;
    }

}
